package com.wanshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanshi.entity.Goods;
import com.wanshi.entity.Supplier;
import com.wanshi.service.IGoodsService;
import com.wanshi.service.ISupplierService;
import com.wanshi.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 供应商信息表 前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-16
 */
@CrossOrigin
@RestController
@RequestMapping("/supplier")
@Api(tags = "测试SupplierController接口文档")
public class SupplierController {

    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private IGoodsService goodsService;

    @ApiOperation(value = "供应商带条件分页")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "ye", value = "页数", paramType = "path", required = true),
                    @ApiImplicitParam(name = "size", value = "一页显示多少条", paramType = "path", required = true)
            }
    )
    @PostMapping("/findPageAndCountSup/{ye}/{size}")
    public Result findPageAndCountSup(@PathVariable int ye, @PathVariable int size, @RequestBody Supplier supplier) {
        Page page = new Page(ye, size);
        QueryWrapper<Supplier> wrapper = new QueryWrapper();
        if (supplier != null) {
            if (!StringUtils.isEmpty(supplier.getName())) {
                wrapper.like("name", supplier.getName());
            }
            if (!StringUtils.isEmpty(supplier.getLinkman())) {
                wrapper.like("linkman", supplier.getLinkman());
            }
            if (!StringUtils.isEmpty(supplier.getMobile())) {
                wrapper.like("mobile", supplier.getMobile());
            }
        }
        wrapper.orderByDesc("id");
        supplierService.page(page, wrapper);
        return Result.ok(page);
    }

    @ApiOperation(value = "添加供应商")
    @PostMapping("addSupplier")
    public Result addSupplier(@RequestBody Supplier supplier) {
        boolean b = supplierService.save(supplier);
        if(b){
            return Result.ok();
        }
        return Result.error("添加供应商失败");
    }

    @ApiOperation(value = "根据id删除供应商")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Integer.class, required = true)
            }
    )
    @DeleteMapping("/{id}")
    public Result deleteSup(@PathVariable String id){
        List<Goods> list = goodsService.findAllBySupId(id);
        if(list.size()>=1){
            return Result.error("该供应商被商品引用，不允许删除");
        }
        boolean b = supplierService.removeById(id);
        if(b==false){
            return Result.error("删除供应商失败");
        }
        return Result.ok();
    }


    @ApiOperation(value = "根据id查找供应商")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Integer.class, required = true)
            }
    )
    @GetMapping("/{id}")
    public Result findByIdSup(@PathVariable String id) {
        return Result.ok(supplierService.getById(id));
    }


    @ApiOperation(value = "修改会员或者添加会员")
    @PostMapping("/supplier")
    public Result UpdateMember(@RequestBody Supplier supplier) {
        boolean b = supplierService.saveOrUpdate(supplier);
        if(b){
            return Result.ok();
        }
        return Result.error("修改供应商失败");
    }


}

